/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.nyquistmeteocal.beans;

import it.nyquistmeteocal.entity.Event;
import it.nyquistmeteocal.boundary.EventManager;
import it.nyquistmeteocal.entity.Notification;
import it.nyquistmeteocal.boundary.NotificationManager;
import it.nyquistmeteocal.boundary.UserManager;
import it.nyquistmeteocal.entity.UserNotification;
import it.nyquistmeteocal.boundary.UserNotificationManager;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Named;

/**
 *
 * @author gatb27
 */
@Named
@RequestScoped
public class DeleteEventBean {

    @EJB
    private EventManager em;
    @EJB
    private NotificationManager nm;
    @EJB
    private UserNotificationManager unm;
    @EJB
    private UserManager um;

    private Event event = new Event();
    private Notification delete = new Notification();
    private String text;
    private Integer notificationid;
    private UserNotification usernotification = new UserNotification();
    private List<String> Recipients;
    private Integer i;
    private Integer count = 0;

    public List<String> getRecipients() {
        return Recipients;
    }

    public void setRecipients(List<String> Recipients) {
        this.Recipients = Recipients;
    }

    public Event getEvent() {
        return event;
    }

    public void setEvent(Event event) {
        this.event = event;
    }

    public Notification getDelete() {
        return delete;
    }

    public void setDelete(Notification delete) {
        this.delete = delete;
    }

    public String DeleteEvent(Integer id, String name, String place, String type,
            String visibility, String creator, String eventdate, String city, String weather) throws SQLException {

        String LoggedUser = um.getLoggedUser().getEmail();
        System.out.println(LoggedUser);
        System.out.println(creator);

        if (LoggedUser.equals(creator)) {
            event.setId(id);
            event.setEventname(name);
            event.setPlace(place);
            event.setEventtype(type);
            event.setVisibility(visibility);
            event.setCreator(creator);
            event.setEventdate(eventdate);
            event.setWeather(weather);
            event.setCity(city);

            ///set id, type and text for the notification
            delete.setEventid(id);
            delete.setType("delete");
            text = "The event called " + name + " scheduled for " + eventdate
                    + " has been deleted! ";
            delete.setText(text);
            System.out.println("NOTIFICATION -> EVENTID: " + delete.getEventid());
            System.out.println("NOTIFICATION -> TYPE " + delete.getType());
            System.out.println("NOTIFICATION -> TEXT " + delete.getText());

            ///get the list of user that have the event in their calendar
            Recipients = nm.getRecipients(id);
            count = Recipients.size();
            System.out.println("the size of the Recipients List IS: " + count);

            if (count != 0) {
                nm.saveNotification(delete);

                notificationid = delete.getId();
                System.out.println("USERNOTIFICATION --> NOTIFICATIONID: " + notificationid);

                ///the event must be deleted after the creation of the notification
                ///due to foreign key constraints
                em.deleteEvent(event);

                ///get the id of the notification that has to be set in the usernotifications
                usernotification.setNotificationid(notificationid);

                ///for every user of the list Recipients, create the usernotifications
                for (i = 0; i < count; i++) {
                    String currentRecipient = Recipients.get(i);
                    usernotification.setUsermail(currentRecipient);
                    unm.saveUserNotification(usernotification);
                }
            } else {
                em.deleteEvent(event);
            }
            return "deleteEvent";
        }

        System.out.println("not creator is trying to delete the event");
        return "/user/event/schedule2";
    }

}
